package com.mobile.liuyi_s.orderlunch.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.test.mock.MockApplication;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.mobile.liuyi_s.orderlunch.MainActivity;
import com.mobile.liuyi_s.orderlunch.MyApplication;
import com.mobile.liuyi_s.orderlunch.R;
import com.mobile.liuyi_s.orderlunch.Utils.Utils;
import com.mobile.liuyi_s.orderlunch.entity.MyUser;
import com.mobile.liuyi_s.orderlunch.entity.Version;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import okhttp3.internal.Util;

public class SlashActivity extends Activity {

    private static final int START_ACITIVY = 1;
    private static final int IS_ACTIVE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_slash);

        mHandler.sendEmptyMessageDelayed(START_ACITIVY, 3000);
        BmobQuery<Version> query = new BmobQuery<>();
        query.addWhereEqualTo("versionCode", MyApplication.VersionCode);
        query.findObjects(new FindListener<Version>() {
            @Override
            public void done(List<Version> list, BmobException e) {
                if (e == null) {
                    if (list.size() != 0) {
                        Message message = new Message();
                        message.what = IS_ACTIVE;
                        message.obj = list;
                        mHandler.sendMessage(message);
                    } else {
                        Toast.makeText(SlashActivity.this, "当前版本错误，请升级应用！", Toast.LENGTH_SHORT).show();
                        SlashActivity.this.finish();
                    }
                } else {
                    Toast.makeText(SlashActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    SlashActivity.this.finish();
                }
            }
        });
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case START_ACITIVY:
                    break;
                case IS_ACTIVE:
                    BmobUser user = MyUser.getCurrentUser();
                    List<Version> list = (List<Version>) msg.obj;
                    if (list.get(0).isActive()) {
                        if (user == null) {
                            startActivity(new Intent(SlashActivity.this, LoginActivity.class));
                            SlashActivity.this.finish();
                        } else {
                            startActivity(new Intent(SlashActivity.this, MainActivity.class));
                            SlashActivity.this.finish();
                        }
                    } else {
                        Toast.makeText(SlashActivity.this, "版本过低，请升级应用！", Toast.LENGTH_SHORT).show();
                        SlashActivity.this.finish();
                    }
                    break;
                default:
                    break;
            }
        }
    };


}
